package servlet.zhouchao;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import pojo.Goods;
import service.GoodsService;
import service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@WebServlet(value = "/PullOutServlet")
public class PullOutServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        String[] title= {"商品ID","商品名称", "商品销量", "商品评价",
                "上架时间", "图片地址", "商品描述","商品成本","商品价格",
                "商品库存","商品状态","商品所属分类id","商品所属分类名称","商品是否参与秒杀"};
        String s=request.getParameter("s");
        String arr[]=s.split("_");
        int goid[]=new int[arr.length];
        for(int i=0;i<arr.length;i++){
            goid[i]=Integer.parseInt(arr[i]);
        }
        List<Goods> list=new ArrayList<>();
        GoodsService goodsService=new GoodsServiceImpl();
        list=goodsService.queryLotGoods(goid);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell;   //列
        for (int i = 0; i < title.length; i++) {
            //循环往第一行列中进行赋值
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        //循环创建其他行并对每行的列增加数据
        for (int i = 0; i < list.size(); i++) {
            HSSFRow nextRow = sheet.createRow(i + 1);
            HSSFCell dataCell = nextRow.createCell(0);
            dataCell.setCellValue(list.get(i).getGoId());
            dataCell = nextRow.createCell(1);
            dataCell.setCellValue(list.get(i).getGoName());
            dataCell = nextRow.createCell(2);
            dataCell.setCellValue(list.get(i).getGoSalesVolume());
            dataCell = nextRow.createCell(3);
            dataCell.setCellValue(list.get(i).getGoPraise());
            dataCell = nextRow.createCell(4);
            dataCell.setCellValue(list.get(i).getGoUptime());
            dataCell = nextRow.createCell(5);
            dataCell.setCellValue(list.get(i).getGoPictureUrl());
            dataCell = nextRow.createCell(6);
            dataCell.setCellValue(list.get(i).getGoDescribe());
            dataCell = nextRow.createCell(7);
            dataCell.setCellValue(list.get(i).getGoCost());
            dataCell = nextRow.createCell(8);
            dataCell.setCellValue(list.get(i).getGoPrice());
            dataCell = nextRow.createCell(9);
            dataCell.setCellValue(list.get(i).getGoStock());
            dataCell = nextRow.createCell(10);
            dataCell.setCellValue(list.get(i).getGoState());
            dataCell = nextRow.createCell(11);
            dataCell.setCellValue(list.get(i).getClId());
            dataCell = nextRow.createCell(12);
            dataCell.setCellValue(list.get(i).getClName());
            dataCell = nextRow.createCell(13);
            dataCell.setCellValue(list.get(i).getGoSecKill());
        }
        OutputStream output=response.getOutputStream();
        response.reset();
        //response.setHeader("Content-disposition", "attachment; filename=customer.xlsx");
        response.setHeader("Content-disposition", "attachment; filename=Goods.xls");//若".xlsx" 文件打不开 就把后缀改为".xls"
        response.setContentType("application/msexcel");
        workbook.write(output);
        output.close();
    }

}
